# _*_ coding:utf-8 _*_

"""
# @Time　 : 2021/12/28 20:52
# @Author : king
# @File　 : excel_handle.py
# @project: ApiPytestFramework
# @QQ     : 772084279
# @Email  : 772084279@qq.com
# @blog   : https://ceshizhilu.blog.csdn.net/
"""
import openpyxl

from common_utils.api_path import data_path
from common_utils.global_vars import CaseEnum


class ReadExcel:

    def __init__(self, filename):
        self.filename = filename

        # 打开文件
        self.workbook = openpyxl.load_workbook(self.filename)

        # 获取sheet
        self.sheets = self.workbook.sheetnames

    def read_excel(self, sheet: str = "") -> list:
        case_data = []
        if sheet == "":
            sheets = self.sheets
        else:
            sheets = [sheet]

        for sheet in sheets:
            wb = self.workbook[sheet]
            max_row = self.workbook[sheet].max_row
            for i in range(2, max_row + 1):
                _dict = {}
                if wb.cell(row=i, column=CaseEnum.API_EXEC.value).value == '是':
                    _dict["id"] = wb.cell(row=i, column=CaseEnum.CASE_ID.value).value
                    _dict["feature"] = wb.cell(row=i, column=CaseEnum.CASE_FEATURE.value).value
                    _dict["title"] = wb.cell(row=i, column=CaseEnum.CASE_TITLE.value).value
                    _dict["url"] = wb.cell(row=i, column=CaseEnum.API_PATH.value).value
                    _dict["header"] = wb.cell(row=i, column=CaseEnum.API_HEADER.value).value
                    _dict["method"] = wb.cell(row=i, column=CaseEnum.API_METHOD.value).value
                    _dict["pk"] = wb.cell(row=i, column=CaseEnum.API_PK.value).value
                    _dict["data"] = wb.cell(row=i, column=CaseEnum.API_DATA.value).value
                    _dict["file"] = wb.cell(row=i, column=CaseEnum.API_FILE.value).value
                    _dict["extract"] = wb.cell(row=i, column=CaseEnum.API_EXTRACT.value).value
                    _dict["validate"] = wb.cell(row=i, column=CaseEnum.API_EXPECTED.value).value

                    case_data.append(_dict)

        return case_data


if __name__ == '__main__':
    print(ReadExcel(data_path + "test_case_data.xlsx").read_excel())
